package com.my.test.java.two.moredata.demo;

import java.sql.SQLException;
import java.util.*;

import com.my.test.java.two.moredata.datas.HDataSource;
import com.my.test.java.two.moredata.datas.rule.DataSourceAg;
import com.my.test.java.two.moredata.datas.rule.DataSourceRule;
import com.my.test.java.two.moredata.datas.rule.TableRule;
import com.my.test.java.two.moredata.datas.rule.TableRuleAg;
import com.my.test.java.two.moredata.datas.share.ShareDataSource;
import com.my.test.java.two.moredata.datas.share.SharePreparedStatement;
import com.my.test.java.two.moredata.datas.share.ShareResultSet;
import com.my.test.java.two.testj.bean.DataSourceBean;

public class App {

	public static Map getDataSource(String url,String user,String pwd,String dataName){
//		DataSourceBean bean = new DataSourceBean(url,user,pwd,"com.mysql.jdbc.Driver",5,10);
//		HDataSource hData = new HDataSource(bean);
		Map<String,HDataSource> map = new HashMap<String,HDataSource>();
		map.put(dataName, new HDataSource(new DataSourceBean(url,user,pwd,"com.mysql.jdbc.Driver",5,10)));
		return map;
	}
	public static TableRuleAg getTableRule(){
		TableRuleAg tableRule = new TableRuleAg();
		tableRule.setCloums(Arrays.asList(new String[]{"student_id"}));
		tableRule.setActualTables(Arrays.asList(new String[]{"t_student_0","t_student_1"}));
		tableRule.setLogicTable("student");
		tableRule.setiTableRule(new TableRule());
		return tableRule;
	}
	public static DataSourceAg getDataRule(){
		DataSourceAg dataRule = new DataSourceAg();
		dataRule.setCloums(Arrays.asList(new String[]{"student_id"}));
		dataRule.setDataNoteList(Arrays.asList(new String[]{"sharding_0","sharding_1"}));
		dataRule.setiDataSourceRule(new DataSourceRule());
		return dataRule;
	}
	public static ShareDataSource getDataSource(){
		String url = "jdbc:mysql://localhost:3306/";
		Map<String,HDataSource> map = new HashMap<String,HDataSource>();
		map.putAll(getDataSource(url+"sharding_0","root","","sharding_0"));
		map.putAll(getDataSource(url+"sharding_1","root","","sharding_1"));
		ShareDataSource s = new ShareDataSource(map,getDataRule(),getTableRule());
		return s;
	}
	
	public void insert(int count) throws SQLException{
		ShareDataSource data = getDataSource();
		if(0==count){
			count = 10;
		}
		String sql = "insert into student(student_id,name,age) values(?,?,?)";
		for(int i=0;i<count;i++){
			SharePreparedStatement ps = data.getConn().prepareStatement(sql);
			ps.setInt(1, i+1);
			ps.setString(2,"hzb");
			ps.setInt(3,15+i);
			int c = ps.executeUpdate();
			System.out.println("----------"+c);
		}
	}
	public static void update(String name,int studentId) throws SQLException{
		ShareDataSource data = getDataSource();
		String sql = "update student set name=?,age=? where student_id=?";
		SharePreparedStatement ps = data.getConn().prepareStatement(sql);
		ps.setString(1, name);
		ps.setInt(2, 188111);
		ps.setInt(3,studentId);
		int c = ps.executeUpdate();
		System.out.println("----------"+c);
	}
	public static void delete(int studentId) throws SQLException{
		ShareDataSource data = getDataSource();
		String sql = "delete from student where student_id=?";
		SharePreparedStatement ps = data.getConn().prepareStatement(sql);
		ps.setInt(1, studentId);
		int c = ps.executeUpdate();
		System.out.println("----------"+c);
	}
	public static void selectAll() throws SQLException{
		ShareDataSource data = getDataSource();
		String sql = "select * from student";
		SharePreparedStatement ps = data.getConn().prepareStatement(sql);
		ShareResultSet rs = ps.executeQuery();
		int i=0;
		while(rs.next()){
			i++;
			System.out.println(rs.getInt("student_id")+","+rs.getString("name"));
		}
		System.out.println("i="+i);
	}
	public static void selectAllOrder() throws SQLException{
		ShareDataSource data = getDataSource();
		String sql = "select * from student order by student_id";
		SharePreparedStatement ps = data.getConn().prepareStatement(sql);
		ShareResultSet rs = ps.executeQuery();
		int i = 0;
		while(rs.next()){
			i++;
			System.out.println(rs.getInt("student_id")+","+rs.getString("name"));
		}
		System.out.println(i);
	}
	public static void selectByStudentId(int studentId) throws SQLException{
		ShareDataSource data = getDataSource();
		String sql = "select * from student where student_id=?";
		SharePreparedStatement ps = data.getConn().prepareStatement(sql);
		ps.setInt(1, studentId);
		ShareResultSet rs = ps.executeQuery();
		while(rs.next()){
			System.out.println(rs.getString("name"));
		}
	}
	
	public static void main(String[] args) {
		try {
//			update("hzb",5);
//			delete(3);
//			selectAll();
			selectAllOrder();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
